package fy.mdm.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import fy.config.FyConfig;
import fy.mdm.bean.NewFindeBean;
import fy.util.DBUtil;

public class NewFindeDao {
	private NewFindeBean newfind = new NewFindeBean();
	private static Log log = LogFactory.getLog(NewFindeDao.class);
	private final static String LOOKNEWFIND = "select * from OACS where Plan_id=?";
	private final static String INSERTNEWFIND = "insert into OACS(Plan_id,Factory_id,Plan_number,Taking_number,Taking_date,LINE_NUMBER,FACTORY,PRODUCT_NAME,PLAN_DATE,PART_NUMBER,BOX_IDENTIFIER,BUCKLE_NUMBER,LIMIT,CEILING,PROCUREMENT,PIECES_NUMBER) values (?,?,?,?,sysdate,?,?,?,?,?,?,?,?,?,?,?)";
	private final static String UPDATENEWFIND = "update OACS set Plan_number=?,Taking_number=? where Plan_id=? and Factory_id=?";

	// 新增
	public boolean NewFindeChange() throws Exception {
		Connection conn = null;
		PreparedStatement pst = null;
		ResultSet rs = null;
		boolean success = false;
		try {
			conn = DBUtil.connect(FyConfig.getOA_DB_CONFIG());

			if (NewFindeCha() == false) {
//				System.err.println("123");
				log.info("新增订单数据");
				pst = conn.prepareStatement(INSERTNEWFIND);
				for (int i = 0; i < this.newfind.getFactory_id().size(); i++) {
					pst.setString(1, this.newfind.getPlan_id());
					pst.setString(2, this.newfind.getFactory_id().get(i));
					pst.setString(3, this.newfind.getPlan_number().get(i));
					pst.setString(4, this.newfind.getTaking_number().get(i));
					pst.setString(5, this.newfind.getLine_Number().get(i));
					pst.setString(6, this.newfind.getFactory().get(i));
					pst.setString(7, this.newfind.getProduct_Name().get(i));
					pst.setString(8, this.newfind.getPlan_Date().get(i));
					pst.setString(9, this.newfind.getPart_number().get(i));
					pst.setString(10, this.newfind.getBox_Identifier().get(i));
					pst.setString(11, this.newfind.getBuckle_number().get(i));
					pst.setString(12, this.newfind.getLimit().get(i));
					pst.setString(13, this.newfind.getCeiling().get(i));
					pst.setString(14, this.newfind.getProcurement().get(i));
					pst.setString(15, this.newfind.getPieces_number().get(i));
//					System.err.println(pst);
					pst.addBatch();
					pst.executeBatch();
				}
			} else {
//				System.err.println("321");
				log.info("修改订单数据");
				pst = conn.prepareStatement(UPDATENEWFIND);
				for (int i = 0; i < this.newfind.getFactory_id().size(); i++) {
					pst.setString(1, this.newfind.getPlan_number().get(i));
					pst.setString(2, this.newfind.getTaking_number().get(i));
					pst.setString(3, this.newfind.getPlan_id());
					pst.setString(4, this.newfind.getFactory_id().get(i));
					pst.addBatch();
					pst.executeBatch();
				}
				conn.commit();
				success = true;
			}

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			DBUtil.close(rs);
			DBUtil.close(pst);
		}
		return success;
	}

	// 查计划单号是否存在
	public boolean NewFindeCha() throws Exception {
		Connection conn = null;
		PreparedStatement pst = null;
		ResultSet rs = null;
		boolean Pk_id = false;
		try {
			conn = DBUtil.connect(FyConfig.getOA_DB_CONFIG());
			pst = conn.prepareStatement(LOOKNEWFIND);
			pst.setString(1, this.newfind.getPlan_id());
			rs = pst.executeQuery();
			if (rs.next()) {
				Pk_id = true;
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			DBUtil.close(rs);
			DBUtil.close(pst);
		}
		return Pk_id;
	}

	public NewFindeBean getNewfind() {
		return newfind;
	}

	public void setNewfind(NewFindeBean newfind) {
		this.newfind = newfind;
	}

}
